package com.ruitu.health_1.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.health_1.R;
import com.ruitu.health_1.mvp.model.CategoryModel;
import com.ruitu.health_1.mvp.presenter.CategoryPresenter;
import com.ruitu.health_1.ui.LoginActivity;
import com.ruitu.health_1.view.PerezosoViewPager;
import com.ruitu.router_module.Logg;
import com.ruitu.router_module.bean.product.ChildrenCategory;

import java.util.ArrayList;
import java.util.List;

public class Category2Fragment extends PerezosoViewPager<CategoryPresenter.Category2ListP, CategoryModel.Category2ListM> {
    private LRecyclerView l_recyclerview;
    private String id;
    private List<ChildrenCategory> childrenCategoryList;
    private List<Integer> skipId = new ArrayList<>();

    public static Category2Fragment newInstance(String id) {
        Bundle args = new Bundle();
        args.putString("data", id);
        Category2Fragment fragment = new Category2Fragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = (String) getArguments().get("data");
    }

    public void setData(List<ChildrenCategory> childrenCategories) {
//        showContent(skipId);
        this.childrenCategoryList = childrenCategories;
        adapter.setDataList(childrenCategoryList);
    }

    public void setError() {

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_category_page, null);
        l_recyclerview = v.findViewById(R.id.l_recyclerview);
        return v;
    }

    private DataAdapter adapter;

    @Override
    protected void onFragmentVisibleChange(boolean isVisibleToUser) {
        super.onFragmentVisibleChange(isVisibleToUser);
        if (isVisibleToUser) {
            p.childrenCategory(id);
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        l_recyclerview.setLayoutManager(new GridLayoutManager(getContext(), 3));
        adapter = new DataAdapter(getContext());
        LRecyclerViewAdapter lRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        l_recyclerview.setAdapter(lRecyclerViewAdapter);
        l_recyclerview.setPullRefreshEnabled(false);
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {

            }
        });
    }

    class DataAdapter extends ListBaseAdapter<ChildrenCategory> {

        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_categoty;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            ChildrenCategory childrenCategory = mDataList.get(position);
            Logg.i("ChildrenCategory:  " + childrenCategory.toString());
            TextView tv_category_name = holder.getView(R.id.tv_category_name);
            tv_category_name.setText(childrenCategory.getName());
        }

    }

}
